package cc.eddic.pratice.programmingparadigms.emp02list.impl;

import cc.eddic.pratice.programmingparadigms.Employee;

import cc.eddic.pratice.programmingparadigms.emp02list.EmployeeList;

public class EmployeeListLhImpl  implements EmployeeList {
    private Employee[] employees;

    public EmployeeListLhImpl(int capacity) {
        employees = new Employee[capacity];
    }

    @Override
    public void add(Employee employee) {

        if (size() == employees.length) {
            Employee[] newemployee = new Employee[employees.length * 2];
            if (size() >= 0) System.arraycopy(employees, 0, newemployee, 0, size());
            employees = newemployee;
        }
        employees[size()] = employee;
    }

    @Override
    public int size() {
        int number = 0;
        for (Employee e : employees) {
            if (e != null) number++;
            else break;
        }
        return number;
    }

    @Override
    public Employee get(int index) {
        if (index >= employees.length || size() == 0 || index >= size()) {
            String s = "Index: " + index + ", Size: " + size();
            throw new IndexOutOfBoundsException(s);
        } else
            return employees[index];

    }

    public EmployeeListLhImpl(Employee[] employeeslist) {
        this(employeeslist.length);
        for (Employee employee : employeeslist) {
            add(employee);
        }
    }
}
